﻿using Autofac;
using Autofac.Integration.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Dependencies;

namespace AppFlat.Common.Ioc
{
    public class AutofacConfig
    {
        public static void AutofacInit()
        {
            var builder = new ContainerBuilder();

            // 三个程序集
            Assembly dalAss = Assembly.Load("AppFlat.Domain.Respostory");
            Assembly bllAss = Assembly.Load("AppFlat.Application.Service");
            Assembly apiAss = Assembly.Load("AppFlat.UI.WebApi");

            builder.RegisterAssemblyTypes(dalAss).PropertiesAutowired().AsImplementedInterfaces();
            builder.RegisterAssemblyTypes(bllAss).PropertiesAutowired().AsImplementedInterfaces();
            builder.RegisterApiControllers(apiAss).PropertiesAutowired();
            
            var container = builder.Build();

            // webapi 和mvc的写法区别
            GlobalConfiguration.Configuration.DependencyResolver
                = new AutofacWebApiDependencyResolver(container);
        }
    }
}
